//may be you need no extern "C" as it fixed in new versions
extern "C" {
#include <hubbub/hubbub.h>
#include <hubbub/parser.h>
}

#include <iostream>
#include <fstream>

static void *myrealloc( void *ptr, size_t len, void* /*pw*/ )
{
    return realloc(ptr, len);
}

static hubbub_error print_tag( const hubbub_token *token, void* pw )
{
    unsigned int*   shift_size  =   static_cast<unsigned int*>(pw);

    if (HUBBUB_TOKEN_START_TAG == token->type) {
        hubbub_tag tag = token->data.tag;
        *shift_size  +=  1;
        for (unsigned int i = 0; i != *shift_size; ++i) {
            std::cout << " ";
        }

        if (tag.self_closing) {
            *shift_size -= 1;
        }

        std::cout << "<";
        std::cout.write(tag.name.ptr, tag.name.len);
        std::cout << ">";
        std::cout << std::endl;
    }
    else if (HUBBUB_TOKEN_END_TAG == token->type) {
        hubbub_tag tag = token->data.tag;
        for (unsigned int i = 0; i != *shift_size; ++i) {
            std::cout << " ";
        }
        *shift_size -= 1;
        std::cout << "</";
        std::cout.write(tag.name.ptr, tag.name.len);
        std::cout << ">";
        std::cout << std::endl;
    }
    else if(HUBBUB_TOKEN_CHARACTER == token->type) {
        hubbub_string   text    =   token->data.character;
        if (0 != text.len) {
            for (unsigned int i = 0; i != *shift_size; ++i) {
                std::cout << " ";
            }
            std::cout.write(text.ptr, text.len);
            std::cout << std::endl;
        }
    }

    return HUBBUB_OK;
}

int main(int argC, char** argV)
{
    //in first argument pass a file test/data/Aliases from hubbub distribution
    hubbub_initialise(argV[1], myrealloc, NULL);

    hubbub_parser*   parser =   NULL;
    hubbub_parser_create(NULL, false, myrealloc, NULL, &parser);

    //set handlers
    unsigned int            shift_size  =   0;
    hubbub_parser_optparams token_handler_opt;
    token_handler_opt.token_handler.pw      =   static_cast<void*>(&shift_size);
    token_handler_opt.token_handler.handler =   print_tag;

    hubbub_parser_setopt( parser
                        , HUBBUB_PARSER_TOKEN_HANDLER
                        , &token_handler_opt );

    //in first argument pass a file test/data/ from hubbub distribution
    std::ifstream               htmlFile(argV[2]);
    static const unsigned int   kBufferSize =   1024;
    char                        buffer[kBufferSize];
    unsigned int                readSize    =   0;

    while ((readSize = htmlFile.readsome(buffer, kBufferSize))!= 0) {
        hubbub_parser_parse_chunk(parser, buffer, readSize);
    }

    hubbub_parser_completed(parser);
    hubbub_parser_destroy(parser);

    hubbub_finalise(myrealloc, NULL);

}
